package org.schemeway.plugins.schemescript.action;

import kawa.lib.*;
import kawa.standard.*;
import gnu.mapping.*;

import org.eclipse.core.runtime.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.ui.*;
import org.eclipse.ui.actions.*;
import org.eclipse.ui.console.*;
import org.eclipse.ui.console.actions.*;
import org.schemeway.plugins.schemescript.*;
import org.schemeway.plugins.schemescript.editor.*;
import org.schemeway.plugins.schemescript.interpreter.*;

/**
 * @author Nu Echo Inc.
 */
public class StartInterpreterAction extends Action implements IWorkbenchWindowActionDelegate {
	public StartInterpreterAction() {
		setText("Start the Scheme interpreter");
		setToolTipText("Start interpreter");
	}

	public void run() {
		Interpreter interp = SchemeScriptPlugin.getDefault().getInterpreter();
		if (interp == null)
			return;

		interp.start();
		interp.showConsole();

		// clear the console
		IConsole[] consoles = ConsolePlugin.getDefault().getConsoleManager().getConsoles();
		for (IConsole console : consoles) {
			if (console.getName().equals(" [Kawa Interpreter] Embedded Kawa")) {
				new ClearOutputAction((TextConsole) console).run();
				break;
			}
		}

		// reset the environment
		//Environment.setCurrent(new Scheme().getEnvironment());

		// save the file in the active editor
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().doSave(
				new NullProgressMonitor());

		// load the file in the active editor
		new LoadFileAction((SchemeEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.getActiveEditor()).run();
	}

	public void dispose() {
	}

	public void init(IWorkbenchWindow window) {
	}

	public void run(IAction action) {
		run();
	}

	public void selectionChanged(IAction action, ISelection selection) {
	}
}